package sz.szsmk.citizencard.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.IUserInfo;
import sz.szsmk.citizencard.model.network.bean.response.QuerLeisureCardsInfoRes;
import sz.szsmk.citizencard.ui.adapter.LeisureYearCardsDialogAdapter;
import sz.szsmk.citizencard.util.ACache;

/**
 * Created by hyt on 2019/4/18.
 */
public class SelectCardDialog  extends Dialog {

    List<QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean> leisur_cards ;
    ISelectCardsDialog iSelectCardsDialog;

    ImageView iv_close;

    TextView tv_tip;

    RecyclerView rv_list;

    Button btn_cancel;

    Button btn_ok;

    QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean lcTemp;

    LeisureYearCardsDialogAdapter adapter;
    public SelectCardDialog(@NonNull Context context, ISelectCardsDialog iSelectCardsDialog,
                            List<QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean> leisur_cards) {
        this(context, R.style.dialog);
        this.leisur_cards = leisur_cards;
        this.iSelectCardsDialog = iSelectCardsDialog;


        int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);

        View divider = findViewById(divierId);
        divider.setVisibility(View.GONE);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(LayoutInflater.from(getContext()).inflate(R.layout.dialog_leisureyearcards, null));

        iv_close =findViewById(R.id.iv_close);

        tv_tip =findViewById(R.id.tv_tip);

        rv_list =findViewById(R.id.rv_list);

        btn_cancel =findViewById(R.id.btn_cancel);

        btn_ok =findViewById(R.id.btn_ok);


        initView();

    }
    private void initView(){

        adapter = new LeisureYearCardsDialogAdapter(getContext(),leisur_cards);

        rv_list.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_list.setAdapter(adapter);


        adapter.setiLycardsAdatperCallBack(leisur_cardsInfoRes -> {

            lcTemp = leisur_cardsInfoRes;

            btn_ok.setText("确认");
            if(leisur_cardsInfoRes.getAcctType()==0){
                if(leisur_cardsInfoRes.getIsHavingPicture()!=1){
                    btn_ok.setText("添加头像");
                }
            }


        });

        String userinfo = ACache.get(getContext()).getAsString("userinfo");
        IUserInfo.UserExBean mUserInfo = new Gson().fromJson(userinfo, IUserInfo.class).getUserEx();


        if(leisur_cards.size()==1){
            tv_tip.setText("您的休闲年卡卡号如下");
        }else{
            tv_tip.setText("请选择一张您的休闲年卡号，添加至身份码");
        }

        btn_cancel.setOnClickListener(v ->
                SelectCardDialog.this.dismiss());
        iv_close.setOnClickListener(v->
                SelectCardDialog.this.dismiss());
        btn_ok.setOnClickListener(v -> {

            if(lcTemp==null){
                Toast.makeText(getContext(),"请选择一张卡",Toast.LENGTH_LONG).show();
            }else{
                iSelectCardsDialog.selectCallBack(this.lcTemp);
                dismiss();
            }

        });
    }

    protected SelectCardDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, R.style.alertdialog);
    }

    public interface ISelectCardsDialog{

        void selectCallBack(QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean lcTemp);
    }

}